<?php
require_once("ClasseBanco.php");

/**
 * Classe Fornelem - arquivo Fornecedores x Elementares
 *
 * @author Jorge Lauand - 22/11/2010
 *
 * Table: FORNELEM, Owner: SYSDBA 

CREATE TABLE "FORNELEM" 
(
  "FECODFORN"	 VARCHAR(12) NOT NULL,
  "FEORGAO"	 VARCHAR(3) NOT NULL,
  "FECODELEM"	 DOUBLE PRECISION NOT NULL,
  "FEPRECO"	 DOUBLE PRECISION,
  "FEDTCOT"	 TIMESTAMP,
  CONSTRAINT "PK_FORNELEM" PRIMARY KEY ("FECODFORN", "FEORGAO", "FECODELEM") );
  ALTER TABLE "FORNELEM" ADD CONSTRAINT "FK_FORNELEM_TABELE" FOREIGN KEY ("FEORGAO", "FECODELEM") REFERENCES "TABELE" ("TORGAO", "TCOD") ON UPDATE CASCADE ON DELETE CASCADE;
 
 */
class fornelem
{
       public $_fecodforn;
       public $_feorgao;
       public $_fecodelem;
       public $fepreco;
       public $fedtcot;

       function __construct($_fecodforn, $_feorgao, $_fecodelem, $fepreco, $fedtcot) {
           $this->_fecodforn = $_fecodforn;
           $this->_feorgao = $_feorgao;
           $this->_fecodelem = $_fecodelem;
           $this->fepreco = $fepreco;
           $this->fedtcot = $fedtcot;
       }

       public function get_fecodforn() {
           return $this->_fecodforn;
       }

       public function set_fecodforn($_fecodforn) {
           $this->_fecodforn = $_fecodforn;
       }

       public function get_feorgao() {
           return $this->_feorgao;
       }

       public function set_feorgao($_feorgao) {
           $this->_feorgao = $_feorgao;
       }

       public function get_fecodelem() {
           return $this->_fecodelem;
       }

       public function set_fecodelem($_fecodelem) {
           $this->_fecodelem = $_fecodelem;
       }

       public function getFepreco() {
           return $this->fepreco;
       }

       public function setFepreco($fepreco) {
           $this->fepreco = $fepreco;
       }

       public function getFedtcot() {
           return $this->fedtcot;
       }

       public function setFedtcot($fedtcot) {
           $this->fedtcot = $fedtcot;
       }

       
       public function montaFornelem($condicao) 
       {
            $sql  = " SELECT FECODFORN,  FEORGAO,   FECODELEM, FEPRECO,  FEDTCOT, TDESC, TUNID, TTIPO, ";
            $sql .= "        FDESCRICAO, FENDERECO, FTELEFONE, FCONTATO, FUF,     FOUTROS, FREGIAO, FATIVO";
            $sql .= " FROM   FORNELEM ";            
            $sql .= " Left   Join TabForn On  FECODFORN  = FCODIGO AND FTIPO   = 'F' ";
            $sql .= " Left   Join TabEle  On  FECODELEM  = TCOD    AND FEORGAO = TORGAO";
            $sql .= $condicao;
            $ba = new bancoDados();
            return $ba->executaQuery($sql);
        }

        public function montaFornelemConsulta($condicao) 
        {
            $sql  = " SELECT FECODFORN,  FEORGAO,   FECODELEM, FEPRECO,  FEDTCOT, TDESC, TUNID, TTIPO, ";
            $sql .= "        FDESCRICAO, FENDERECO, FTELEFONE, FCONTATO, FUF,     FOUTROS, FREGIAO, FATIVO";
            $sql .= " FROM   FORNELEM ";            
            $sql .= " LEFT   Join TabForn On  FECODFORN  = FCODIGO AND FTIPO   = 'F' ";
            $sql .= " LEFT   Join TabEle  On  FECODELEM  = TCOD    AND FEORGAO = TORGAO";
            $sql .= $condicao;

            $ba = new bancoDados();
            return $ba->executaQueryConsulta($sql);
        }

        public function incluirFornelem() 
        {
            $ba = new bancoDados();
            $sql = $ba->montaIncluirSQL($this);
            $ba->atualizaQuery($sql);
            return true;
        }

        public function alterarFornelem() 
        {
            $ba = new bancoDados();
            $sql = $ba->montaAtualizarSQL($this);
            $ba->atualizaQuery($sql);
            return true;
        }

        public function excluiFornelem() 
        {
            $ba = new bancoDados();
            $sql = $ba->montaExcluirSQL($this);
            $ba->atualizaQuery($sql);
            return true;
        }

        public function XMLFornelem($condicao) {

            $rs = $this->montaFornelemConsulta($condicao);

            header("Content-type:application/xml;");
            $xml = "<?xml version='1.0' encoding='iso-8859-1'?>\r\n";

            global $xml;

            $xml .= "<fornelems>";

            if ( $rs != '' )
            {
                foreach ($rs as $row) {
                    $xml .= "<fornelem>";
                    $xml .=     "<fecodforn>"  . Trim(utf8_encode($row['FECODFORN'])) . "</fecodforn>";
                    $xml .=     "<feorgao>"    . Trim(utf8_encode($row['FEORGAO']))   . "</feorgao>";
                    $xml .=     "<fecodelem>"  . number_format($row['FECODELEM'], 0)  . "</fecodelem>";
                    $xml .=     "<fepreco>"    . number_format($row['FEPRECO'], 2)    . "</fepreco>";
                    $xml .=     "<fedtcot>"    . mostraData($row['FEDTCOT'])          . "</fedtcot>";                    
                    $xml .=     "<tdesc>"      . Trim(utf8_encode( ereg_replace('>', " ", ereg_replace('<', " ",    ereg_replace('"', "'", $row['TDESC']) ) )  )) . "</tdesc>";  //__substitui " por '
                    $xml .=     "<tunid>"      . Trim(utf8_encode($row['TUNID']))     . "</tunid>";
                    $xml .=     "<ttipo>"      . Trim(utf8_encode($row['TTIPO']))     . "</ttipo>";
                    $xml .=     "<fdescricao>" . Trim(utf8_encode( ereg_replace('>', " ", ereg_replace('<', " ",    ereg_replace('"', "'", $row['FDESCRICAO']) ) )  )) . "</fdescricao>";
                    $xml .=     "<fendereco>"  . Trim(utf8_encode($row['FENDERECO'])) . "</fendereco>";
                    $xml .=     "<ftelefone>"  . Trim(utf8_encode($row['FTELEFONE'])) . "</ftelefone>";
                    $xml .=     "<fcontato>"   . Trim(utf8_encode($row['FCONTATO']))  . "</fcontato>";
                    $xml .=     "<fuf>"        . Trim(utf8_encode($row['FUF']))       . "</fuf>";
                    $xml .=     "<foutros>"    . Trim(utf8_encode($row['FOUTROS']))   . "</foutros>";
                    $xml .=     "<fregiao>"    . Trim(utf8_encode($row['FREGIAO']))   . "</fregiao>";
                    $xml .=     "<fativo>"     . Trim(utf8_encode($row['FATIVO']))    . "</fativo>";
                    $xml .= "</fornelem>";
                }
            }
            $xml .= '</fornelems>';
            echo $xml;
            return;
        }
}
?>